package guiobjects;

import java.awt.Color;
import java.util.Scanner;

/**
 * Draws a straight line.
 * 
 * @author Trevor Terris
 *
 */
public class Line implements IPaintedObject {
	private double startX = 0;
	private double startY = 0;
	private double endX = 0;
	private double endY = 0;
	private int r = 0;
	private int g = 0;
	private int b = 0;
	private int myWidth = 0;
	public void parseText(String input)
	{
		Scanner s = new Scanner(input);
		startX = s.nextDouble();
		startY = s.nextDouble();
		endX = s.nextDouble();
		endY = s.nextDouble();
		if (s.hasNextInt())
		{
			r = s.nextInt();
			g = s.nextInt();
			b = s.nextInt();
		}
		if (s.hasNextInt())
		{
			myWidth = s.nextInt();
			myWidth = (myWidth > 5)?5:myWidth;
			myWidth = (myWidth < 0)?0:myWidth;
		}
	}
	
	public void drawObject(java.awt.Graphics pen, int screenWidth, int screenHeight, int offset)
	{
		pen.setColor(new Color(r,g,b));
		pen.drawLine((int)(startX * screenWidth) + offset, (int)(startY * screenHeight), 
					 (int)(endX * screenWidth) + offset, (int)(endY * screenHeight));
		for(int i = 0; i < myWidth; i++)
		{
			int xMult = 0;
			int yMult = 1;
			
			double xDiff = (endX * screenWidth) - (startX * screenWidth);
			double yDiff = (endY * screenWidth) - (startY * screenWidth);
			double slope = -1 * yDiff/xDiff;
			
			if (slope > 1 || slope < -1)
			{
				xMult = 1;
				yMult = 0;
			}
			
			pen.drawLine((int)(startX * screenWidth) - i * xMult + offset, (int)(startY * screenHeight) - i * yMult, 
						 (int)(endX * screenWidth) - i * xMult + offset, (int)(endY * screenHeight) - i * yMult);
			
			pen.drawLine((int)(startX * screenWidth) + i * xMult + offset, (int)(startY * screenHeight) + i * yMult, 
						 (int)(endX * screenWidth) + i * xMult + offset, (int)(endY * screenHeight) + i * yMult);
		}
	}
	public String getSVGData(int width, int offset, int total)
	{
		String output = "<line x1=\"";
		double startXValue = (((double)offset + (double)width * startX) * 100.0 / (double) total);
		double endXValue = (((double)offset + (double)width * endX) * 100.0 / (double) total);
		double startYValue = (startY * 100.0);
		double endYValue = (endY * 100.0);
		
		output += startXValue + "%\" y1=\"" + startYValue + "%\" x2=\"" + endXValue + "%\"  y2=\"" + endYValue;
		output += "%\" style=\"stroke:rgb(" + r + "," + g + "," + b + ");stroke-width:" + (myWidth * 2 - 1) + "\"/>";
		
		return output;
	}
}
